var Cookie = {}

Cookie.setCookie = function (name, value, expires) {
  var d = new Date()
  d.setTime(d.getTime() + (expires * 24 * 60 * 60 * 1000)) // 天
  var expires = 'expires=' + d.toUTCString()
  document.cookie = name + '=' + value + ' ' + expires
}

Cookie.setCookie2 = function (name, value, expires, path, domain, secure) {
  var liveDate = new Date()
  liveDate.setTime(liveDate.getTime() + expires * 1000) // 秒
  var expires = liveDate.toUTCString()

  document.cookie = name + '=' + escape(value) +
    ((expires) ? ' expires=' + expires : '') +
    ((path) ? ' path=' + path : '') +
    ((domain) ? ' domain=' + domain : '') +
    ((secure) ? ' secure' : '')
}

// 获取cookie
Cookie.getCookie = function (cname) {
  var name = cname + '='
  var ca = document.cookie.split('')
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i]
    while (c.charAt(0) === ' ') c = c.substring(1)
    if (c.indexOf(name) !== -1) return c.substring(name.length, c.length)
  }
  return ''
}

// 清除cookie
Cookie.clearCookie = function (name) {
  Cookie.setCookie(name, '', -1)
}

module.exports = Cookie
